package com.kla.power.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kla.power.config.LogPointer;
import com.kla.power.entity.FileEntity;
import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.entity.StationEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IFileService;
import com.kla.power.service.IStationService;
import com.kla.power.service.IUserService;
import com.kla.power.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 发电站表 前端控制器
 * </p>
 * @author WuZH
 * @since 2024-06-02
 */
@RestController
@RequestMapping ("/station")
@Api ("发电站接口")
@CrossOrigin (value = "*", maxAge = 3600)
@Slf4j
public class StationController {

    @Resource
    private IStationService iStationService;
    @Resource
    private IUserService iUserService;
    @Resource
    private IFileService iFileService;


    @RequestMapping ("queryList")
    @ApiOperation ("PC端查询所有电站列表")
    @ApiImplicitParams ( {@ApiImplicitParam (name = "stationEntity", value = "电站对象", dataType = "StationEntity对象")})
    @LogPointer ("查询电站列表")
    public ResultBody queryList (@RequestBody StationEntity stationEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( stationEntity.getToken () );
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        stationEntity.setType ( ConstUtil.STATION_MAIN_TYPE );
        List<StationEntity> stationEntities = iStationService.queryList ( stationEntity );
        return ResultBody.success ( stationEntities, iStationService.queryCount ( stationEntity ) );
    }

    @RequestMapping ("queryCount")
    @LogPointer ("查询电站总数")
    public ResultBody queryCount (@RequestBody StationEntity stationEntity) {
        Integer count = iStationService.queryCount ( stationEntity );
        return ResultBody.success ( count );
    }

    /**
     * 查询电站详情
     * @param stationEntity
     * @return
     */
    @RequestMapping ("querySingle")
    @LogPointer ("查询电站详情")
    public ResultBody querySingle (@RequestBody StationEntity stationEntity) {
        Map<String, Object> map = new HashMap<> ();
        stationEntity = iStationService.querySingle ( stationEntity );
        UserEntity userEntity = new UserEntity ();
        userEntity.setId ( stationEntity.getUserId () );
        userEntity = iUserService.querySingle ( userEntity );
        map.put ( "stationEntity", stationEntity );
        map.put ( "userEntity", userEntity );
        return ResultBody.success ( map );
    }


    /**
     * 新增电站，当新建子电站的时候，前端拿主电站Id，作为子电站的parentId即可
     * @param stationEntity
     * @return
     */
    @RequestMapping ("saveSingle")
    @ApiOperation ("保存电站")
    @ApiImplicitParams ( {@ApiImplicitParam (name = "stationEntity", value = "电站对象", dataType = "StationEntity对象")})
    @LogPointer ("保存电站")
    public ResultBody saveSingle (@RequestBody StationEntity stationEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( stationEntity.getToken () );
        stationEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        stationEntity.setUserId ( userEntity.getId () );
        stationEntity.setMerchantId ( userEntity.getMerchantId () );
        stationEntity = iStationService.saveSingle ( stationEntity );
        return ResultBody.success ( stationEntity );
    }

    @RequestMapping ("updateSingle")
    @ApiOperation ("修改电站")
    @LogPointer ("修改电站")
    public ResultBody updateSingle (@RequestBody StationEntity stationEntity) {
        stationEntity = iStationService.updateSingle ( stationEntity );
        return ResultBody.success ( stationEntity );
    }

    /**
     * 电站子系统查询单个电站
     * @param stationEntity
     * @return
     */
    @RequestMapping ("child")
    @ApiOperation ("查询单个电站")
    @LogPointer ("查询电站子系统")
    @ApiImplicitParams ( {@ApiImplicitParam (name = "stationEntity", value = "电站对象", dataType = "StationEntity对象")})
    public ResultBody child (@RequestBody StationEntity stationEntity) {
        Map<String, Object> map = iStationService.childStation ( stationEntity );
        // 通过当前电站参数Id，查询所有子电站
        StationEntity station = new StationEntity ();
        station.setParentId ( stationEntity.getId () );
        station.setType ( ConstUtil.STATION_CHILD_TYPE );
        List<StationEntity> stationEntities = iStationService.queryList ( station );
        map.put ( "childStation", stationEntities );
        // 并网和购网
        map.put ( "fpFKwh", "0" );
        map.put ( "rfpFKwh", "0" );
        return ResultBody.success ( map );
    }


    /**
     * 子系统列表
     * @param stationEntity
     * @return
     */
    @RequestMapping ("childList")
    @LogPointer ("子系统列表")
    public ResultBody childList (@RequestBody StationEntity stationEntity) {
        List<StationEntity> stationEntities = iStationService.getBaseMapper ().selectList ( new QueryWrapper<StationEntity> ().eq ( "parentId", stationEntity.getId () ) );
        return ResultBody.success ( stationEntities );
    }

    /**
     * 查询电站历史数据：日月年发电量、发电功率、满发小时
     * @param stationEntity
     * @return
     */
    @RequestMapping ("stationHistoryData")
    @LogPointer ("子系统发电历史查询")
    public ResultBody stationHistoryData (@RequestBody StationEntity stationEntity) {
        Map<String, Object> map = iStationService.childHistoryData ( stationEntity );
        return ResultBody.success ( map );
    }


    /**
     * 逆变器排名：满发小时排名=逆变器发电量/电站容量
     * @param stationEntity
     * @return
     */
    @RequestMapping ("inverterRanking")
    @LogPointer ("子系统逆变器排名")
    public ResultBody inverterRanking (@RequestBody StationEntity stationEntity) {
        List<InverterElectricEntity> inverterSystemEntities = iStationService.inverterRanking ( stationEntity );
        return ResultBody.success ( inverterSystemEntities );
    }


    /**
     * 上传电站里的图片
     * @param stationEntity
     * @return
     */
    @RequestMapping ("uploadImages")
    @LogPointer ("电站图片上传")
    public ResultBody uploadImages (@RequestBody StationEntity stationEntity) {
        if ( stationEntity.getId () == null || stationEntity.getFileEntities () == null || stationEntity.getFileEntities ().size () == 0 ) {
            return ResultBody.fail ( StateEnum.PRIMARY_NULL_EXCE );
        }
        for ( FileEntity fileEntity : stationEntity.getFileEntities () ) {
            fileEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
            fileEntity.setType ( ConstUtil.FILE_TYPE_STATION );
            fileEntity.setRelationId ( stationEntity.getId () );
            iFileService.saveSingle ( fileEntity );
        }
        return ResultBody.success ( stationEntity );
    }

    /**
     * 查询当前电站下所有图片列表
     * @param stationEntity
     * @return
     */
    @RequestMapping ("imagesList")
    @LogPointer ("电站图片列表")
    public ResultBody imagesList (@RequestBody StationEntity stationEntity) {
        FileEntity fileEntity = new FileEntity ();
        fileEntity.setRelationId ( stationEntity.getId () );
        fileEntity.setType ( ConstUtil.FILE_TYPE_STATION );
        List<FileEntity> fileEntities = iFileService.queryList ( fileEntity );
        return ResultBody.success ( fileEntities );
    }

    /**
     * 删除电站下属图片
     * @param fileEntity
     * @return
     */
    @RequestMapping ("deleteImage")
    @LogPointer ("删除电站图片")
    public ResultBody deleteImage (@RequestBody FileEntity fileEntity) {
        fileEntity = iFileService.deleteSingle ( fileEntity );
        return ResultBody.success ( fileEntity );
    }

}
